package com.hnqc.wukoy;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class Client {
    private DefaultListModel dt;
    private JTextArea jta;
    private JTextField jtf_message = new JTextField(30);
    private JTextField jtf_port = new JTextField("3245");
    private JTextField jtf_max = new JTextField("30");
    private JTextField jtf_name = new JTextField("");
    private JButton jbt_start = new JButton("连接");
    private JButton jbt_stop = new JButton("停止");
    private List<Socket> list= new ArrayList<Socket>();
    private Socket socket;
    //UI设置方法
    public void createUI() {
        JFrame jf = new JFrame("客户端");
        jf.setBounds(200, 200, 600, 300);
        //北边Jpanel
        JPanel northPane = new JPanel();
        northPane.setLayout(new GridLayout(1, 8));
        northPane.add(new JLabel("人数上限"));
        northPane.add(jtf_max);
        northPane.add(new JLabel("端口"));
        northPane.add(jtf_port);
        northPane.add(new JLabel("姓名"));
        northPane.add(jtf_name);
        northPane.add(jbt_start);
        jbt_stop.setEnabled(false);
        northPane.add(jbt_stop);
        northPane.setBorder(new TitledBorder("配置信息"));
        //中间Jpanl(左右分栏)
        //左栏
        dt = new DefaultListModel();
        JList jl = new JList(dt);
        JScrollPane leftpane = new JScrollPane(jl);
        leftpane.setBorder(new TitledBorder("在线用户"));
        //右栏
        jta = new JTextArea();
        jta.setEnabled(false);
        JScrollPane reghtpane = new JScrollPane(jta);
        reghtpane.setBorder(new TitledBorder("消息显示区"));
        JSplitPane centPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftpane, reghtpane);
        //设置分割条大小位置
        centPane.setDividerSize(20);
        centPane.setDividerLocation(100);
        //南部Jpanel
        JPanel southPanel = new JPanel();
        southPanel.setBorder(new TitledBorder("写消息"));
        southPanel.add(jtf_message, BorderLayout.CENTER);
        jf.setLayout(new BorderLayout());
        jf.add(northPane, BorderLayout.NORTH);
        jf.add(centPane, BorderLayout.CENTER);
        jf.add(southPanel, BorderLayout.SOUTH);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);
        myEevent();
    }

    private void myEevent() {
        jtf_message.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
//                OutputStream outputStream = socket.getOutputStream();
//                PrintStream printStream=new PrintStream(outputStream);
//                printStream.println(socket.getInetAddress()+","+socket.getPort()+","+jtf_nameText);

            }
        });

        jtf_message.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {

                if (e.getKeyChar() == KeyEvent.VK_ENTER) {

                    try {
                        OutputStream out=socket.getOutputStream();
                        PrintWriter pw = new PrintWriter(out,true);
                        pw.println(jtf_message.getText().trim());
                        jtf_message.setText("");

                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        });

        jbt_start.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                String port=jtf_port.getText().trim();
//                int port_new=Integer.parseInt(port);
                try {
                    socket = new Socket("127.0.0.1",9999);
                    //客户端连接服务器，告诉服务器自己的信息（
                    //获取姓名 ip 端口号
                    String jtf_nameText = jtf_name.getText();
                    InetAddress  ip=socket.getInetAddress();
                    int  port=socket.getLocalPort();
                    dt.addElement(jtf_nameText);



                    //告诉 通过流写过去
                    OutputStream outputStream = socket.getOutputStream();
                    PrintStream printStream=new PrintStream(outputStream);
                    printStream.println(jtf_nameText+","+ip+","+port);

                    ClientReaderThread clientReaderThread=new ClientReaderThread(socket);
                    new Thread(clientReaderThread).start();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        });
    }

    class ClientReaderThread implements Runnable{
        private Socket socket;
        public ClientReaderThread(Socket socket){
            this.socket=socket;
        }
        @Override
        public void run() {
            try {
                InputStream in=socket.getInputStream();
                BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(in));
                while (true){
                    String str=bufferedReader.readLine();
                    jta.append("服务器说："+str+"\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        new Client().createUI();
    }
}


